﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using Billing.Controls;
using Billing.Utilities;
using System.Configuration;

namespace Billing
{
    public partial class Form1 : Form
    {
        PaperSize psize;

        public Form1()
        {
            InitializeComponent();
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            double totalQty = 0;
            if (Storage.IsFirstBill)
            {
                e.PageSettings.PaperSize = psize;
                e.Graphics.DrawString("KS BAKERS", new Font(new FontFamily("Times New Roman"), 13, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 140, 0);
                e.Graphics.DrawString("BHAGYA NAGAR COLONY. PH 000000000", new Font(new FontFamily("Times New Roman"), 13, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 20);
                e.Graphics.DrawString("TIN NO.28952015564", new Font(new FontFamily("Times New Roman"), 13, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 100, 40);
            }
            else
            {
                e.PageSettings.PaperSize = psize;
                e.Graphics.DrawString("KS BAKERS", new Font(new FontFamily("Times New Roman"), 13, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 140, 0);
                e.Graphics.DrawString(ConfigurationManager.AppSettings["ColonyName"], new Font(new FontFamily("Times New Roman"), 12, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, 20);
                e.Graphics.DrawString(ConfigurationManager.AppSettings["TinNo"], new Font(new FontFamily("Times New Roman"), 12, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 100, 40);
                if (Storage.CreditNoteCompany == null || Storage.CreditNoteCompany == string.Empty)
                    e.Graphics.DrawString("Cashier Name: " + Storage.LoginUser, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 60);
                else
                    e.Graphics.DrawString("Cashier Name: " + Storage.LoginUser + "              Bill For: " + Storage.CreditNoteCompany, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 60);

                //if (Storage.CreditNoteCompany == null || Storage.CreditNoteCompany == string.Empty)
                if (Storage.BillDate == string.Empty)
                    e.Graphics.DrawString("Date : " + DateTime.Now.ToString("dd/MM/yyyy") + "     " + DateTime.Now.ToShortTimeString() + "          Bill No : " + Storage.BillNo, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 75);
                else
                    e.Graphics.DrawString("Date : " + Storage.BillDate + "          Bill No : " + Storage.BillNo, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 75);
                //else
                //    e.Graphics.DrawString("Date : " + DateTime.Now.ToString("dd/MM/yyyy") + "     Time : " + DateTime.Now.ToShortTimeString() + "          Bill For " + Storage.CreditNoteCompany, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 75);

                e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 90);
                e.Graphics.DrawString("Item Name                         Rate            Qty           Amount", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, 105);
                e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 120);

                int lineCounter = 135;
                double totalAmt = 0d;

                if (PrintCommon.PrintItems != null && PrintCommon.PrintItems.Count > 0)
                {
                    foreach (BillingGridItemControl item in PrintCommon.PrintItems)
                    {
                        if (item.txtCode.Text != string.Empty)
                        {
                            double totalPrice = double.Parse(item.txtPrice.Text);
                            double qty = double.Parse(item.txtQty.Text);
                            totalQty += qty;
                            totalAmt += totalPrice;

                            e.Graphics.DrawString(item.txtDesc.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                            e.Graphics.DrawString((totalPrice / qty).ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 185, lineCounter);
                            e.Graphics.DrawString(item.txtQty.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 275, lineCounter);
                            e.Graphics.DrawString(item.txtPrice.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 340, lineCounter);

                            lineCounter += 15;
                        }
                    }
                    e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                    lineCounter += 15;
                    e.Graphics.DrawString("Items: " + PrintCommon.PrintItems.Count + "                                            Total Qty: " + totalQty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                    lineCounter += 20;
                }
                else if (PrintCommon.CreditNoteItems != null && PrintCommon.CreditNoteItems.Count > 0)
                {
                    foreach (CreditNoteBillingGridItemControl item in PrintCommon.CreditNoteItems)
                    {
                        if (item.txtCode.Text != string.Empty)
                        {
                            double totalPrice = double.Parse(item.txtPrice.Text);
                            double qty = double.Parse(item.txtQty.Text);
                            totalQty += qty;
                            totalAmt += totalPrice;

                            e.Graphics.DrawString(item.txtDesc.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                            e.Graphics.DrawString((totalPrice / qty).ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 185, lineCounter);
                            e.Graphics.DrawString(item.txtQty.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 275, lineCounter);
                            e.Graphics.DrawString(item.txtPrice.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 340, lineCounter);

                            lineCounter += 15;
                        }
                    }
                    e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                    lineCounter += 15;
                    e.Graphics.DrawString("Items: " + PrintCommon.CreditNoteItems.Count + "                                            Total Qty: " + totalQty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                    lineCounter += 20;
                }
                else if (PrintCommon.BulkOrderItems != null && PrintCommon.BulkOrderItems.Count > 0)
                {
                    foreach (BulkOrderBillingGridItemControl item in PrintCommon.BulkOrderItems)
                    {
                        if (item.txtCode.Text != string.Empty)
                        {
                            double totalPrice = double.Parse(item.txtPrice.Text);
                            double qty = double.Parse(item.txtQty.Text);
                            totalQty += qty;
                            totalAmt += totalPrice;

                            e.Graphics.DrawString(item.txtDesc.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                            e.Graphics.DrawString((totalPrice / qty).ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 185, lineCounter);
                            e.Graphics.DrawString(item.txtQty.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 275, lineCounter);
                            e.Graphics.DrawString(item.txtPrice.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 340, lineCounter);

                            lineCounter += 15;
                        }
                    }
                    e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                    lineCounter += 15;
                    e.Graphics.DrawString("Items: " + PrintCommon.BulkOrderItems.Count + "                                            Total Qty: " + totalQty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                    lineCounter += 20;
                }
                else if (PrintCommon.VendorItems != null && PrintCommon.VendorItems.Count > 0)
                {
                    foreach (VendorBillingGridItemControl item in PrintCommon.VendorItems)
                    {
                        if (item.txtCode.Text != string.Empty)
                        {
                            double totalPrice = double.Parse(item.txtPrice.Text);
                            double qty = double.Parse(item.txtQty.Text);
                            totalQty += qty;
                            totalAmt += totalPrice;

                            e.Graphics.DrawString(item.txtDesc.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                            e.Graphics.DrawString((totalPrice / qty).ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 185, lineCounter);
                            e.Graphics.DrawString(item.txtQty.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 275, lineCounter);
                            e.Graphics.DrawString(item.txtPrice.Text, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 340, lineCounter);

                            lineCounter += 15;
                        }
                    }
                    e.Graphics.DrawString("------------------------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                    lineCounter += 15;
                    e.Graphics.DrawString("Items: " + PrintCommon.VendorItems.Count + "                                            Total Qty: " + totalQty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                    lineCounter += 20;
                }

                //e.Graphics.DrawString("Total Payable: " + Storage.PayableAmt + "/-", new Font(new FontFamily("Times New Roman"), 14, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 210, lineCounter);

                if (Storage.DiscountByPercentage != null && Storage.DiscountByPercentage != string.Empty)
                {
                    e.Graphics.DrawString("Discount @ " + Storage.DiscountByPercentage + " % = -" + Math.Round(double.Parse(Storage.DiscountedAmt) / 100 * PrintCommon.TotalAmt, 2) + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter - 5);
                    e.Graphics.DrawString("Total Amount: " + PrintCommon.TotalAmt + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 220, lineCounter - 5);
                    e.Graphics.DrawString("Total Payable: " + Math.Round(PrintCommon.TotalAmt - (double.Parse(Storage.DiscountByPercentage) / 100) * PrintCommon.TotalAmt, 2) + "/-", new Font(new FontFamily("Times New Roman"), 14, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 210, lineCounter + 10);
                }
                else if (PrintCommon.Discount != 0)
                {
                    e.Graphics.DrawString("Discount in RS = -" + Storage.DiscountedAmt + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter - 5);
                    e.Graphics.DrawString("Total Amount: " + Storage.TotalAmount + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 220, lineCounter - 5);
                    e.Graphics.DrawString("Total Payable: " + (PrintCommon.TotalAmt - PrintCommon.Discount) + "/-", new Font(new FontFamily("Times New Roman"), 14, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 210, lineCounter + 10);
                }
                else
                {
                    e.Graphics.DrawString("Total Payable: " + PrintCommon.TotalAmt + "/-", new Font(new FontFamily("Times New Roman"), 14, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 210, lineCounter);
                }

                if (Storage.CashAmt != 0d)
                {
                    lineCounter += 15;
                    e.Graphics.DrawString("Cash : " + (PrintCommon.TotalAmt - PrintCommon.Discount) + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                }
                if (Storage.CardAmt != 0d)
                {
                    lineCounter += 15;
                    e.Graphics.DrawString("Card : " + Storage.CardAmt.ToString() + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                }
                if (Storage.SodexhoAmt != 0d)
                {
                    lineCounter += 15;
                    e.Graphics.DrawString("Sodexho : " + Storage.SodexhoAmt.ToString() + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                }

                lineCounter += 10;
                e.Graphics.DrawString(string.Empty, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
                if (PrintCommon.BulkOrderItems != null && PrintCommon.BulkOrderItems.Count > 0)
                    e.Graphics.DrawString("    BULK", new Font(new FontFamily("Times New Roman"), 45, System.Drawing.FontStyle.Regular), new SolidBrush(Color.FromArgb(64, Color.Black)), 10, 0);
            }
        }

        private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
        {
            if (Storage.IsFirstBill)
            {
                psize = new PaperSize("saurabh", 400, 75);
                e.PageSettings.PaperSize = psize;
            }
            else
            {
                if (PrintCommon.PrintItems != null && PrintCommon.PrintItems.Count != 0)
                {
                    psize = new PaperSize("saurabh", 400, 315 + PrintCommon.PrintItems.Count * 15);
                    e.PageSettings.PaperSize = psize;
                }
                else if (PrintCommon.CreditNoteItems != null && PrintCommon.CreditNoteItems.Count != 0)
                {
                    psize = new PaperSize("saurabh", 400, 315 + PrintCommon.CreditNoteItems.Count * 15);
                    e.PageSettings.PaperSize = psize;
                }
                else if (PrintCommon.BulkOrderItems != null && PrintCommon.BulkOrderItems.Count != 0)
                {
                    psize = new PaperSize("saurabh", 400, 315 + PrintCommon.BulkOrderItems.Count * 15);
                    e.PageSettings.PaperSize = psize;
                }
                else if (PrintCommon.VendorItems != null && PrintCommon.VendorItems.Count != 0)
                {
                    psize = new PaperSize("saurabh", 400, 315 + PrintCommon.VendorItems.Count * 15);
                    e.PageSettings.PaperSize = psize;
                }
            }
        }
    }
}
